package com.konke.sdslibrary.factroy;

import android.content.Context;
import android.text.TextUtils;

import com.konke.sdslibrary.ISdsDomain;
import com.konke.sdslibrary.net.ICloud;
import com.konke.sdslibrary.net.IKonkeCloud;
import com.konke.sdslibrary.net.RetrofitHttpBuilder;
import com.konke.sdslibrary.net.request.BindInfoRequest;
import com.konke.sdslibrary.net.request.RegisterDeviceReq;
import com.konke.sdslibrary.net.request.UpdatePushTokenReq;
import com.konke.sdslibrary.net.response.BaseResp;
import com.konke.sdslibrary.net.response.BindQrCodeResp;
import com.konke.sdslibrary.net.response.CCUBindInfo;
import com.zsj.comminlibrary.util.GetDeviceInfoResp;
import com.konke.sdslibrary.net.response.RegisterDeviceResp;
import com.konke.sdslibrary.net.response.VersionResp;
import com.konke.sdslibrary.util.PhoneUtil;
import com.zsj.comminlibrary.util.SpManager;
import com.zsj.comminlibrary.util.log.XLog;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import retrofit2.Response;

public class SdsDomain implements ISdsDomain {

    private CcuHostManager mCcuHostManager;
    private Context mContext;
    private String pushToken;
    private static SdsDomain sDomain;
    private ICloud mICloud;
    private final String product_admin_id = "2801861";
    private final String product_admin_token = "29f8ebfe-e46a-4564-9a4e-9f1007ed79d5";
    private String mUUID = "";
    private String mAccessToken;
    private String mId;
    private SpManager mSpManager;
    private boolean hasUpdatePush = false;


    private SdsDomain() {
    }

    public static SdsDomain getInstance() {
        if (sDomain == null) {
            sDomain = new SdsDomain();
        }
        return sDomain;
    }

    @Override
    public void init(Context context) {
        mContext = context;
        mCcuHostManager = new CcuHostManager(mContext);
        mICloud = RetrofitHttpBuilder.buildICloud(ICloud.BASE_URL);
        mSpManager = SpManager.getInstance();
        mSpManager.init(mContext);
        mId = mSpManager.getDevId();
        mAccessToken = mSpManager.getDevToken();
    }

    @Override
    public BaseResp<RegisterDeviceResp> registerDevice() {
        BaseResp<RegisterDeviceResp> resp = new BaseResp<>();
        try {
            String sn = PhoneUtil.getSn(mContext);
            if (sn == null || TextUtils.isEmpty(sn)) {
                sn = mUUID = UUID.randomUUID().toString().replace("-", "");
                mSpManager.putSn(sn);
                XLog.e("设备sn号获取失败,临时uuid: " + sn);
            }
            Response<RegisterDeviceResp> response = mICloud.registerDevice(product_admin_id, product_admin_token, new RegisterDeviceReq(sn)).execute();
            if (response != null && response.isSuccessful()) {
                resp.setSuccess(true);
                mId = response.body().getId();
                mAccessToken = response.body().getAccessToken();
                mSpManager.putDevId(mId);
                mSpManager.putDevToken(mAccessToken);
                resp.setData(response.body());
                updatePushToken();
                return resp;
            }
            resp.setSuccess(false);
            if (response == null) {
                resp.setMsg("response == null");
            } else {
                resp.setMsg(response.message());
                resp.setErrorCode(response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resp;
    }

    @Override
    public BaseResp<BindQrCodeResp> bindQrCode() {
        BaseResp<BindQrCodeResp> resp = new BaseResp<>();
        try {
            Response<BindQrCodeResp> response = mICloud.bindQrCode(mId, mAccessToken, mId).execute();
            if (response != null && response.isSuccessful()) {
                resp.setSuccess(true);
                resp.setData(response.body());
                return resp;
            }
            resp.setSuccess(false);
            if (response == null) {
                resp.setMsg("response == null");
            } else {
                resp.setMsg(response.message());
                resp.setErrorCode(response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resp;
    }

    @Override
    public BaseResp<GetDeviceInfoResp> getDevInfo() {
        BaseResp<GetDeviceInfoResp> resp = new BaseResp<>();
        try {
            Response<GetDeviceInfoResp> response = mICloud.getDeviceInfo(mId, mAccessToken, mId).execute();
            if (response != null && response.isSuccessful()) {
                SpManager instance = SpManager.getInstance();
                instance.putDeviceInfo(response.body());
                resp.setSuccess(true);
                resp.setData(response.body());
                return resp;
            }
            resp.setSuccess(false);
            if (response == null) {
                resp.setMsg("response == null");
            } else {
                resp.setMsg(response.message());
                resp.setErrorCode(response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resp;
    }

    @Override
    public void updatePushToken(String token) {
        this.pushToken = token;
        if (!hasUpdatePush) {
            updatePushToken();
        }
    }

    public BaseResp updatePushToken() {
        BaseResp<RegisterDeviceResp> resp = new BaseResp<>();
        if (pushToken == null || TextUtils.isEmpty(pushToken)) {
            resp.setSuccess(false);
            resp.setMsg("pushToken is empty");
            return resp;
        }
        hasUpdatePush = true;
        try {
            String sn = PhoneUtil.getSn(mContext);
            if (sn == null || TextUtils.isEmpty(sn)) {
                sn = mUUID = UUID.randomUUID().toString().replace("-", "");
                XLog.e("设备sn号获取失败,临时uuid: " + sn);
            }
            UpdatePushTokenReq req = new UpdatePushTokenReq();
            req.setPush_channel(3);
            req.setPush_client_id(pushToken);
            req.setPush_service_type(1);
            Response<Void> response = mICloud.updatePushToken(mId, mAccessToken, mId, req).execute();
            if (response != null && response.isSuccessful()) {
                resp.setSuccess(true);
                return resp;
            }
            resp.setSuccess(false);
            if (response == null) {
                resp.setMsg("response == null");
            } else {
                resp.setMsg(response.message());
                resp.setErrorCode(response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resp;
    }


    @Override
    public CCUBindInfo getCCUBindInfo(String ccuName) {
        try {
            Response<CCUBindInfo> response = mICloud.getCCUBindInfo(new BindInfoRequest(ccuName)).execute();
            if (response != null) {
                if (response.isSuccessful()) {
                    return response.body();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getAccessToken() {
        return mAccessToken;
    }

    public String getId() {
        return mId;
    }

    public CcuHostManager getCcuHostManager() {
        return mCcuHostManager;
    }

    @Override
    public List<GetDeviceInfoResp.BindCcuBean.ThirdPartDevicesBean.YsDeviceInfoListBean> getCameraDevices(int roomId) {
        List<GetDeviceInfoResp.BindCcuBean.ThirdPartDevicesBean.YsDeviceInfoListBean> list = new ArrayList<>();
        GetDeviceInfoResp.BindCcuBean.ThirdPartDevicesBean devices = mSpManager.getDeviceInfo().getBindCcu().getThirdPartDevices();
        if (devices != null && devices.getYsDeviceInfoList() != null && devices.getYsDeviceInfoList().size() > 0) {
            List<GetDeviceInfoResp.BindCcuBean.ThirdPartDevicesBean.YsDeviceInfoListBean> ysDeviceInfoList = devices.getYsDeviceInfoList();
            for (GetDeviceInfoResp.BindCcuBean.ThirdPartDevicesBean.YsDeviceInfoListBean bean : ysDeviceInfoList) {
                if (roomId == -1 || (roomId + "").equals(bean.getBindRoomId())) {
                    list.add(bean);
                }
            }
        }
        return list;
    }

    @Override
    public BaseResp<VersionResp> getAppVersion() {
        BaseResp<VersionResp> resp = new BaseResp<>();
        try {
            IKonkeCloud iKonkeCloud = RetrofitHttpBuilder.buildKonkeCloud(IKonkeCloud.BASE_URL);
            Response<VersionResp> response = iKonkeCloud.getAppVersion().execute();
            if (response != null && response.isSuccessful()) {
                resp.setSuccess(true);
                resp.setData(response.body());
                return resp;
            }
            resp.setSuccess(false);
            if (response == null) {
                resp.setMsg("response == null");
            } else {
                resp.setMsg(response.message());
                resp.setErrorCode(response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resp;
    }
}
